package com.example.sample.activity;

import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.example.app.BaseActivity;
import com.example.sample.R;
import com.example.sample.logic.CodecWrapper;

public class MediaCodecActivity extends BaseActivity {

    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private CodecWrapper wrapper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        wrapper.stop();
    }

    private void prepare(){
        if (wrapper == null){
            wrapper = new CodecWrapper(surfaceView, "/sdcard/a.mp4");
        }
    }

    private void initView(){
        setContentView(R.layout.activity_media_codec);
        surfaceView = (SurfaceView) findViewById(R.id.surface);
        findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wrapper.start();
            }
        });
        findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wrapper.stop();
            }
        });

        surfaceHolder = surfaceView.getHolder();
        // 设置Holder类型,该类型表示surfaceView自己不管理缓存区,虽然提示过时，但最好还是要设置
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        // 设置surface回调
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                prepare();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });

        surfaceView.setZOrderOnTop(true);
        surfaceHolder.setFormat(PixelFormat.TRANSPARENT);
    }


}
